package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.wcs.service.system.entity.RgvMapDB;
import com.suray.wcs.service.system.mapper.RgvMapDBMapper;
import com.suray.wcs.service.system.service.IRgvMapDBService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cooper
 * @since 2019-06-26
 */
@Service
public class RgvMapDBServiceImpl extends ServiceImpl<RgvMapDBMapper, RgvMapDB> implements IRgvMapDBService {
    @Resource
    private RgvMapDBMapper rgvMapDBMapper;
    @Override
    public void deleteByWarehouseId(int warehouseId) {
        QueryWrapper<RgvMapDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RgvMapDB::getWarehouseId, warehouseId);
        remove(queryWrapper);
    }

    @Override
    public void deleteByLayer(int warehouseId, int layer) {
        QueryWrapper<RgvMapDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RgvMapDB::getWarehouseId, warehouseId)
                .eq(RgvMapDB::getLayer, layer);
        remove(queryWrapper);
    }

    @Override
    public void updateMapLayer(int warehouseId, String map, int layer) {
        LambdaUpdateWrapper<RgvMapDB> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RgvMapDB::getWarehouseId, warehouseId)
                .eq(RgvMapDB::getMap, map)
                .set(RgvMapDB::getLayer, layer);

        if (!update(updateWrapper)) {
            throw new RuntimeException("更新地图层失败：" + map + ", " + layer + "层");
        }
    }
    @Override
    public int updateMap(RgvMapDB rgvMapDB) {
        return baseMapper.updateById(rgvMapDB);
    }
    @Override
    public void updateMapName(int warehouseId, String map, int layer) {
        LambdaUpdateWrapper<RgvMapDB> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RgvMapDB::getWarehouseId, warehouseId)
                .eq(RgvMapDB::getLayer, layer)
                .set(RgvMapDB::getMap, map);

        if (!update(updateWrapper)) {
            throw new RuntimeException("更新地图名称失败：" + map + ", " + layer + "层");
        }
    }

    @Override
    public List<RgvMapDB> selectMapList(RgvMapDB rgvMapDB) {
        LambdaQueryWrapper<RgvMapDB> lambdaQueryWrapper = new LambdaQueryWrapper();
//        if (rgvMapDB.getLayer()!=null) {
//            lambdaQueryWrapper.eq(rgvMapDB::getLayer, rgvMapDB.getLayer());
//        }
        return rgvMapDBMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 新增数据
     *
     * @param rgvMapDB 实例对象
     * @return 新增结果
     */
    @Override
    public boolean insertWcsDist(RgvMapDB rgvMapDB) {
        RgvMapDB rgvMapDB1 = rgvMapDB;

        return this.save(rgvMapDB1);
    }


}
